package com.beautiful.xin.sort;

import io.reactivex.rxjava3.core.Maybe;

/**
 * @author xuqingxin 2021/9/5 4:57 下午
 */
public class ReverseTest {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        ListNode l5 = new ListNode(5);
        l4.next = l5;
        l3.next = l4;
        l2.next= l3;
        l1.next = l2;

        reverseBetween(l1, 2, 4);
    }

    public static ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode t = new ListNode(0);
        t.next = head;
        ListNode pre = t;
        for (int i = 0; i < left - 1; i++) {
            pre = pre.next;
        }

        ListNode cur = pre.next;
        ListNode next;
        for (int i = 0; i < right - left; i++) {
            next = cur.next;
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
        }
        return t.next;

    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
